/* eslint-disable no-unused-vars */
import React, { useEffect } from "react";
import styles from './launch.module.css';
import { useHistory } from "react-router-dom/cjs/react-router-dom";
import { useDispatch } from "react-redux";
import icon_app_logo from "@/assets/icon_app_logo.png"
import { isLogin } from "@/utils/storage";
import { isLoanLoaning, isLoanReject, isLoanRepayNow, isLoanReviewing, isLoanSigning } from "@/utils/loanStatus";
import { PageTag } from "@/utils/pageTag";
import AppParams from "@/utils/appParams";
import { reqAppConfig, reqSaveEvent, reqUserState } from "@/store/actions/apiServices";

/**
 * 启动页
 */
export default function Launch() {

    //判断是否登录
    const checkLogin = isLogin()
    console.log("checkLogin: " + checkLogin)

    // 获取路由历史 history 对象
    const history = useHistory()

    //获取 Redux 分发器
    const dispatch = useDispatch()


    const onReqUserStateCallback = (success, resObj) => {
        if (success && resObj) {
            //是否为签约中
            const isHomeSigning = isLoanSigning(resObj.signFlag, resObj.signOrderId);
            //是否为审核中
            const isHomeReviewing = isLoanReviewing(resObj.applyStatus);
            //是否为拒绝
            const isHomeReject = isLoanReject(resObj.applyStatus, resObj.orderStatus);
            //是否为放款中
            const isHomeLoaning = isLoanLoaning(resObj.orderStatus);
            //是否为待还款
            const isHomeRepay = isLoanRepayNow(resObj.orderStatus);
            //是否为待进件
            const isHomeNotLoan = ((!isHomeReviewing) && (!isHomeReject) && (!isHomeLoaning) && (!isHomeRepay));
            console.log("Launch-isHomeReviewing: " + isHomeReviewing);
            console.log("Launch-isHomeReject: " + isHomeReject);
            console.log("Launch-isHomeLoaning: " + isHomeLoaning);
            console.log("Launch-isHomeRepay: " + isHomeRepay);
            console.log("Launch-isHomeNotLoan: " + isHomeNotLoan);
            console.log("Launch-isHomeSigning: " + isHomeSigning);

            if (isHomeSigning) {
                history.replace('/guide');
            } else if (isHomeReviewing) {
                history.replace('/review');
            } else if (isHomeReject) {
                history.replace('/reject');
            } else if (isHomeLoaning) {
                history.replace('/loaning');
            } else if (isHomeRepay) {
                history.replace('/repay');
            } else {
                history.replace('/home');
            }
        }
    };

    useEffect(() => {
        reqSaveEvent("LAUNCH_START")

        reqAppConfig((success, resObj) => {
            if (AppParams.tid.length > 0) {
                reqUserState(PageTag.LAUNCH_REFERRER, onReqUserStateCallback);
                // SmartChat.init();

            } else if (checkLogin) {
                reqUserState(PageTag.LAUNCH, onReqUserStateCallback);
                // SmartChat.init();

            } else {
                history.replace("/loginad")
            }
        });

        return () => {
            reqSaveEvent("LAUNCH_END")
        }
    }, [])


    return (
        <div className={styles.launchRoot}>
            <img
                className={styles.launchImageSmall}
                src={icon_app_logo}
                alt="" />
            <span className={styles.launchTitle}>Onua Credit</span>
        </div>
    )
}